package com.sdk.pay.transfer.api;

import com.sdk.pay.transfer.api.params.DESParams;
import com.sdk.pay.transfer.api.params.Params;
import com.sdk.pay.transfer.listener.CallBack;
import com.sdk.pay.transfer.model.WECHATBean;
import com.sdk.pay.transfer.constant.PayContext;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;


/**
 * Created by Administrator on 2017/9/6.
 */

public class PostQrCodeListAPI extends APISHelper {
    private static final String URL = "api/insertqr";
    private static final String TAG = "PostQrCodeAPI";
    private List<WECHATBean> mModels = new ArrayList<>();

    public void postQrCodeList(List<WECHATBean> models, final Listener listener) {
        mModels.clear();
        mModels.addAll(models);
        try {
            post(TAG, URL, new CallBack() {
                @Override
                public void onSuccess(Call call, String response) throws IOException {
                    try {
                        JSONObject object = new JSONObject(response);
                        if (object.getString("flag").equals(PayContext.TASK_SUCCESS)) {
                            if (listener != null)
                                listener.onSuccess(object.getString("msg"));
                        } else {
                            if (listener != null)
                                listener.onFailure(-1, object.getString("msg"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        if (listener != null) {
                            listener.onFailure(-1, e.getMessage());
                        }
                    }
                }

                @Override
                public void onError(int code) {
                    if (listener != null)
                        listener.onFailure(code, "");
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
            if (listener != null)
                listener.onFailure(-1, e.getMessage());
        }
    }

    @Override
    protected Params initParams() throws JSONException {
        Params params = new DESParams();
        putArray(params, PayContext.UPLOAD_LIST_KEY, mModels);
        return params;
    }

    public void putArray(Params params, String key, List<WECHATBean> models) throws JSONException {
        JSONArray mArray = new JSONArray();
        JSONObject object = null;
        for (WECHATBean model : models) {
            object = new JSONObject();
            object.put("syscode", PayContext.SYSTEM);
            object.put("money", model.getMoney());
            object.put("reason", model.getReason());
            object.put("url", model.getCodeUrl());
            object.put("imgtype", "3");
            mArray.put(object);
        }
        params.put(key, mArray);
    }

    public interface Listener {
        void onSuccess(String message);

        void onFailure(int code, String message);
    }
}
